//
//  CollectViewController.m
//  ITrip
//
//  Created by dllo on 15/6/4.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"
#import "UMSocial.h"
#import <UIImageView+WebCache.h>
#import "Database.h"
#import "TravelNotesSecondViewController.h"
#import "SubjectSecondViewController.h"
#import "JourneyViewController.h"
#import "CollectionTableViewCell.h"
@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain) UITableView *collecTableView;

@property(nonatomic, retain) NSMutableArray *arrayData;



@end

@implementation CollectViewController

- (void)dealloc
{
    
    [_arrayData release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 110, 30)];
    title.text = @"  我的收藏";
    title.font = [UIFont systemFontOfSize:20];
    title.font = [UIFont boldSystemFontOfSize:25];
    title.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = title;
    
    UIImage *icon = [UIImage imageNamed:@"iconfont_return"];
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleDone target:self action:@selector(getBack)];
    
    self.arrayData = [NSMutableArray array];
    self.collecTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.collecTableView];
    self.collecTableView.delegate = self;
    self.collecTableView.dataSource = self;
    self.collecTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.collecTableView release];
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.image = [UIImage imageNamed:@"375-600_BG"];
    [self.collecTableView setBackgroundView:bgView];
    
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width * 1 / 3.0)];
    headerView.backgroundColor = [UIColor yellowColor];
    headerView.image = [UIImage imageNamed:@"375-136_view"];
//    [self.view addSubview:headerView];
    
//    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

-  (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.collecTableView setEditing:editing animated:animated];
}

- (void)getBack
{
    self.tabBarController.tabBar.hidden  = NO;
    self.tabBarController.tabBar.translucent = NO;
    [self.navigationController popViewControllerAnimated:YES];
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        return @"我的收藏";
//    }
//    return nil;
//}

- (void)viewDidAppear:(BOOL)animated
{
    NSMutableArray *array = [NSMutableArray arrayWithArray:[Database selectData]];
    self.arrayData = array;
    [self.collecTableView reloadData];
    if (self.arrayData.count == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
        label.text = @"您还没有收藏！";
        [self.view addSubview:label];
        label.font = [UIFont systemFontOfSize:23];
        label.font = [UIFont boldSystemFontOfSize:25];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor grayColor];
        label.center = self.view.center;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[CollectionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    DbModel *model = [self.arrayData objectAtIndex:indexPath.section];
//    cell.textLabel.text = model.title;
////    [cell.imageView setImageWithURL:[NSURL URLWithString:model.imageURL] placeholderImage:nil];
//    
//    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    [cell setDbModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrayData.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DbModel *model = [self.arrayData objectAtIndex:indexPath.section];
    NSLog(@"%ld", model.choose);
    if (model.choose == 0) {
        TravelNotesSecondViewController *travel = [[TravelNotesSecondViewController alloc] init];
        travel.number = model.titleId;
        [self.navigationController pushViewController:travel animated:YES];
        [travel release];
    } else if (model.choose == 1) {
        SubjectSecondViewController *subject = [[SubjectSecondViewController alloc] init];
        subject.number = model.titleId;
        [self.navigationController pushViewController:subject animated:YES];
        //        [subject release];
    } else if (model.choose == 2) {
        JourneyViewController *journey = [[JourneyViewController alloc] init];
        journey.number = model.titleId;
        journey.isStrategy = 1;
        [self.navigationController pushViewController:journey animated:YES];
        [journey release];
    }
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        DbModel *model = [self.arrayData objectAtIndex:indexPath.section];
        NSLog(@"%ld", model.titleId);
        [Database deleteValue:model.titleId];
        [self.arrayData removeObjectAtIndex:indexPath.section];
//        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
        [self.collecTableView reloadData];
        if (self.arrayData.count == 0) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
            label.text = @"您还没有收藏！";
            [self.view addSubview:label];
            label.font = [UIFont systemFontOfSize:23];
            label.font = [UIFont boldSystemFontOfSize:25];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = [UIColor grayColor];
            label.center = self.view.center;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
